//1.导入需要用的框架和包
const express=require('express')
const path=require('path')
var app=express()
var router=require('./router')
var bodyParser=require('body-parser')//中间件 获取post表单提交数据

//2.配置插件,静态公共资源
app.use('/public',express.static(path.join(__dirname,'public')))
app.use('/node_modules',express.static(path.join(__dirname,'node_modules')))
app.use(bodyParser.urlencoded({ extended: false })) //配置中间件
app.use(bodyParser.json())  //配置中间件


app.use(function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');

  if (req.method == 'OPTIONS') {
    res.send(200); /*让options请求快速返回*/
  } else {
    next();
  }
})

app.use(router)//把路由挂载到app.js




app.use(function(req,res){ //配置404页面
	res.send('404')
})

app.use(function(err,req,res,next){//全局处理错误方法
	res.status(500).json({
		err_code:500,
		message:err.message
	})
})

app.listen('2080',function(){ //开启服务器监听
	console.log('server is running 2080 ...')
})